package action;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import basic.PageInfo;
import basic.QuerySet;
import basic.ResultSet;
import basic.SortInfo;
import common.BaseAction;
import common.dict.CommonDict;
import entities.TCard;
import interfaces.ICardService;
import util.ParameterUtil;
import utils.DateUtil;
import utils.ExcelUtil;
import utils.StringUtil;

public class CardAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 7507534255609348887L;

	private ICardService cardService;

	private TCard cardBean;
	private List<TCard> cardList;

	private String crdNo;
	private String crdNos;
	private String crdIp;
	private String crdOs;
	private String crdBrowser;
	private String crdFinger;
	private String crdLocale;
	private String crdTiming;
	private String crdCode;
	private String crdBin;
	private String crdType;
	private String crdFake;
	private String crdNumber;
	private String crdExpire;
	private String crdCvv;
	private String crdCfname;
	private String crdClname;
	private String crdCphone;
	private String crdCemail;
	private String crdCcountry;
	private String crdCstate;
	private String crdCcity;
	private String crdCaddr;
	private String crdCzip;
	private String crdHfname;
	private String crdHlname;
	private String crdHphone;
	private String crdHemail;
	private String crdHcountry;
	private String crdHstate;
	private String crdHcity;
	private String crdHaddr;
	private String crdHzip;
	private String crdState;
	private Date crdDate;
	private Date crdDateTop;
	private Date crdDateEnd;
	private String crdRemark;

	// 展示对象
	public String show() throws Exception {
		if (!cardService.exist(crdNo)) {
			addActionError("数据不存在");
			return INFO;
		} else {
			cardBean = cardService.fetch(crdNo);
			return SUCCESS;
		}
	}

	// 编辑对象
	public String edit() throws Exception {
		if (!cardService.exist(crdNo)) {
			cardBean = new TCard();
		} else {
			cardBean = cardService.fetch(crdNo);
		}
		return SUCCESS;
	}

	// 持久化对象
	public String save() throws Exception {
		if (!cardService.exist(crdNo)) {
			cardBean = new TCard();
		} else {
			cardBean = cardService.fetch(crdNo);
		}
		cardBean.setCrdNo(crdNo);
		cardBean.setCrdIp(crdIp);
		cardBean.setCrdOs(crdOs);
		cardBean.setCrdBrowser(crdBrowser);
		cardBean.setCrdFinger(crdFinger);
		cardBean.setCrdLocale(crdLocale);
		cardBean.setCrdTiming(crdTiming);
		cardBean.setCrdCode(crdCode);
		cardBean.setCrdBin(crdBin);
		cardBean.setCrdType(crdType);
		cardBean.setCrdFake(crdFake);
		cardBean.setCrdNumber(crdNumber);
		cardBean.setCrdCfname(crdCfname);
		cardBean.setCrdClname(crdClname);
		cardBean.setCrdCphone(crdCphone);
		cardBean.setCrdCemail(crdCemail);
		cardBean.setCrdCcountry(crdCcountry);
		cardBean.setCrdCstate(crdCstate);
		cardBean.setCrdCcity(crdCcity);
		cardBean.setCrdCaddr(crdCaddr);
		cardBean.setCrdCzip(crdCzip);
		cardBean.setCrdHfname(crdHfname);
		cardBean.setCrdHlname(crdHlname);
		cardBean.setCrdHphone(crdHphone);
		cardBean.setCrdHemail(crdHemail);
		cardBean.setCrdHcountry(crdHcountry);
		cardBean.setCrdHstate(crdHstate);
		cardBean.setCrdHcity(crdHcity);
		cardBean.setCrdHaddr(crdHaddr);
		cardBean.setCrdHzip(crdHzip);
		cardBean.setCrdState(crdState);
		cardBean.setCrdDate(crdDate);
		cardBean.setCrdRemark(crdRemark);

		cardService.persist(cardBean);
		return SUCCESS;
	}

	// 列表展示
	public String list() throws Exception {
		QuerySet<Object> querySet = new QuerySet<Object>();

		HashMap<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("crdNo", StringUtil.split(crdNo));
		paramMap.put("crdIp", crdIp);
		paramMap.put("crdOs", crdOs);
		paramMap.put("crdBrowser", crdBrowser);
		paramMap.put("crdFinger", crdFinger);
		paramMap.put("crdLocale", crdLocale);
		paramMap.put("crdTiming", crdTiming);
		paramMap.put("crdCode", crdCode);
		paramMap.put("crdBin", crdBin);
		paramMap.put("crdType", crdType);
		paramMap.put("crdFake", crdFake);
		paramMap.put("crdNumber", crdNumber);
		paramMap.put("crdCfname", crdCfname);
		paramMap.put("crdClname", crdClname);
		paramMap.put("crdCphone", crdCphone);
		paramMap.put("crdCemail", crdCemail);
		paramMap.put("crdCcountry", crdCcountry);
		paramMap.put("crdCstate", crdCstate);
		paramMap.put("crdCcity", crdCcity);
		paramMap.put("crdCaddr", crdCaddr);
		paramMap.put("crdCzip", crdCzip);
		paramMap.put("crdHfname", crdHfname);
		paramMap.put("crdHlname", crdHlname);
		paramMap.put("crdHphone", crdHphone);
		paramMap.put("crdHemail", crdHemail);
		paramMap.put("crdHcountry", crdHcountry);
		paramMap.put("crdHstate", crdHstate);
		paramMap.put("crdHcity", crdHcity);
		paramMap.put("crdHaddr", crdHaddr);
		paramMap.put("crdHzip", crdHzip);
		paramMap.put("crdState", crdState);
		paramMap.put("crdDate", ParameterUtil.dateCombin(crdDateTop, crdDateEnd));
		paramMap.put("crdRemark", crdRemark);

		SortInfo sortInfo = new SortInfo();
		sortInfo.setSortField((StringUtil.isEmpty(sortField)) ? "crdDate" : sortField);
		sortInfo.setSortOrder(sortOrder);

		querySet.setPageInfo(new PageInfo(pageIndex, pageSize));
		querySet.setSortInfo(sortInfo);
		querySet.setParamMap(paramMap);

		ResultSet<TCard> resultSet = cardService.search(querySet);
		cardList = resultSet.getResultList();
		pageInfo = resultSet.getPageInfo();
		return SUCCESS;
	}

	// 导出数据 查询条件等同于列表查询条件
	public String export() throws Exception {
		QuerySet<Object> querySet = new QuerySet<Object>();

		HashMap<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("crdNo", StringUtil.split(crdNo));
		paramMap.put("crdIp", crdIp);
		paramMap.put("crdOs", crdOs);
		paramMap.put("crdBrowser", crdBrowser);
		paramMap.put("crdFinger", crdFinger);
		paramMap.put("crdLocale", crdLocale);
		paramMap.put("crdTiming", crdTiming);
		paramMap.put("crdCode", crdCode);
		paramMap.put("crdBin", crdBin);
		paramMap.put("crdType", crdType);
		paramMap.put("crdFake", crdFake);
		paramMap.put("crdNumber", crdNumber);
		paramMap.put("crdCfname", crdCfname);
		paramMap.put("crdClname", crdClname);
		paramMap.put("crdCphone", crdCphone);
		paramMap.put("crdCemail", crdCemail);
		paramMap.put("crdCcountry", crdCcountry);
		paramMap.put("crdCstate", crdCstate);
		paramMap.put("crdCcity", crdCcity);
		paramMap.put("crdCaddr", crdCaddr);
		paramMap.put("crdCzip", crdCzip);
		paramMap.put("crdHfname", crdHfname);
		paramMap.put("crdHlname", crdHlname);
		paramMap.put("crdHphone", crdHphone);
		paramMap.put("crdHemail", crdHemail);
		paramMap.put("crdHcountry", crdHcountry);
		paramMap.put("crdHstate", crdHstate);
		paramMap.put("crdHcity", crdHcity);
		paramMap.put("crdHaddr", crdHaddr);
		paramMap.put("crdHzip", crdHzip);
		paramMap.put("crdState", crdState);
		paramMap.put("crdDate", ParameterUtil.dateCombin(crdDateTop, crdDateEnd));
		paramMap.put("crdRemark", crdRemark);

		SortInfo sortInfo = new SortInfo();
		sortInfo.setSortField((StringUtil.isEmpty(sortField)) ? "crdDate" : sortField);
		sortInfo.setSortOrder(sortOrder);

		querySet.setPageInfo(new PageInfo(1, CommonDict.EXPORT_LIMIT));
		querySet.setSortInfo(sortInfo);
		querySet.setParamMap(paramMap);

		ResultSet<TCard> resultSet = cardService.search(querySet);
		cardList = resultSet.getResultList();

		List<String> fieldHead = new ArrayList<String>();
		fieldHead.add("编号");
		fieldHead.add("ip地址");
		fieldHead.add("操作系统");
		fieldHead.add("浏览器");
		fieldHead.add("指纹");
		fieldHead.add("语言");
		fieldHead.add("时区");
		fieldHead.add("ip国家码");
		fieldHead.add("bin国家码");
		fieldHead.add("类型");
		fieldHead.add("伪卡号");
		fieldHead.add("卡号");
		fieldHead.add("收货人firstname");
		fieldHead.add("收货人lastname");
		fieldHead.add("收货人电话");
		fieldHead.add("收货人邮件");
		fieldHead.add("收货人国家");
		fieldHead.add("收货人州/省");
		fieldHead.add("收货人城市");
		fieldHead.add("收货人地址");
		fieldHead.add("收货人邮编");
		fieldHead.add("持卡人firstname");
		fieldHead.add("持卡人lastname");
		fieldHead.add("持卡人电话");
		fieldHead.add("持卡人邮箱");
		fieldHead.add("持卡人国家");
		fieldHead.add("持卡人州/省");
		fieldHead.add("持卡人城市");
		fieldHead.add("持卡人地址");
		fieldHead.add("持卡人邮编");
		fieldHead.add("状态");
		fieldHead.add("日期日期");
		fieldHead.add("日期时间");
		fieldHead.add("备注");

		List<List<String>> fieldData = new ArrayList<List<String>>();

		for (TCard cardBean : cardList) {
			List<String> fieldItem = new ArrayList<String>();
			fieldItem.add(cardBean.getCrdNo());
			fieldItem.add(cardBean.getCrdIp());
			fieldItem.add(cardBean.getCrdOs());
			fieldItem.add(cardBean.getCrdBrowser());
			fieldItem.add(cardBean.getCrdFinger());
			fieldItem.add(cardBean.getCrdLocale());
			fieldItem.add(cardBean.getCrdTiming());
			fieldItem.add(cardBean.getCrdCode());
			fieldItem.add(cardBean.getCrdBin());
			fieldItem.add(cardBean.getCrdType());
			fieldItem.add(cardBean.getCrdFake());
			fieldItem.add(cardBean.getCrdNumber());
			fieldItem.add(cardBean.getCrdCfname());
			fieldItem.add(cardBean.getCrdClname());
			fieldItem.add(cardBean.getCrdCphone());
			fieldItem.add(cardBean.getCrdCemail());
			fieldItem.add(cardBean.getCrdCcountry());
			fieldItem.add(cardBean.getCrdCstate());
			fieldItem.add(cardBean.getCrdCcity());
			fieldItem.add(cardBean.getCrdCaddr());
			fieldItem.add(cardBean.getCrdCzip());
			fieldItem.add(cardBean.getCrdHfname());
			fieldItem.add(cardBean.getCrdHlname());
			fieldItem.add(cardBean.getCrdHphone());
			fieldItem.add(cardBean.getCrdHemail());
			fieldItem.add(cardBean.getCrdHcountry());
			fieldItem.add(cardBean.getCrdHstate());
			fieldItem.add(cardBean.getCrdHcity());
			fieldItem.add(cardBean.getCrdHaddr());
			fieldItem.add(cardBean.getCrdHzip());
			fieldItem.add((cardBean.getCrdState().equals("0")) ? "无效" : "有效");
			fieldItem.add(DateUtil.format(cardBean.getCrdDate(), DateUtil.DATE));
			fieldItem.add(DateUtil.format(cardBean.getCrdDate(), DateUtil.TIME));
			fieldItem.add(cardBean.getCrdRemark());

			fieldData.add(fieldItem);
		}

		inputStream = ExcelUtil.excel(fieldHead, fieldData);
		return SUCCESS;
	}

	////////////////////// ---------------------------//////////////////////////////////////////////////////////////////////////////////////////////////////////////
	////////////////////// ---------------------------//////////////////////////////////////////////////////////////////////////////////////////////////////////////
	////////////////////// ---------------------------//////////////////////////////////////////////////////////////////////////////////////////////////////////////

	public ICardService getCardService() {
		return cardService;
	}

	public void setCardService(ICardService cardService) {
		this.cardService = cardService;
	}

	public TCard getCardBean() {
		return cardBean;
	}

	public void setCardBean(TCard cardBean) {
		this.cardBean = cardBean;
	}

	public List<TCard> getCardList() {
		return cardList;
	}

	public void setCardList(List<TCard> cardList) {
		this.cardList = cardList;
	}

	public String getCrdNo() {
		return crdNo;
	}

	public void setCrdNo(String crdNo) {
		this.crdNo = crdNo;
	}

	public String getCrdNos() {
		return crdNos;
	}

	public void setCrdNos(String crdNos) {
		this.crdNos = crdNos;
	}

	public String getCrdIp() {
		return crdIp;
	}

	public void setCrdIp(String crdIp) {
		this.crdIp = crdIp;
	}

	public String getCrdOs() {
		return crdOs;
	}

	public void setCrdOs(String crdOs) {
		this.crdOs = crdOs;
	}

	public String getCrdBrowser() {
		return crdBrowser;
	}

	public void setCrdBrowser(String crdBrowser) {
		this.crdBrowser = crdBrowser;
	}

	public String getCrdFinger() {
		return crdFinger;
	}

	public void setCrdFinger(String crdFinger) {
		this.crdFinger = crdFinger;
	}

	public String getCrdLocale() {
		return crdLocale;
	}

	public void setCrdLocale(String crdLocale) {
		this.crdLocale = crdLocale;
	}

	public String getCrdTiming() {
		return crdTiming;
	}

	public void setCrdTiming(String crdTiming) {
		this.crdTiming = crdTiming;
	}

	public String getCrdCode() {
		return crdCode;
	}

	public void setCrdCode(String crdCode) {
		this.crdCode = crdCode;
	}

	public String getCrdBin() {
		return crdBin;
	}

	public void setCrdBin(String crdBin) {
		this.crdBin = crdBin;
	}

	public String getCrdType() {
		return crdType;
	}

	public void setCrdType(String crdType) {
		this.crdType = crdType;
	}

	public String getCrdFake() {
		return crdFake;
	}

	public void setCrdFake(String crdFake) {
		this.crdFake = crdFake;
	}

	public String getCrdNumber() {
		return crdNumber;
	}

	public void setCrdNumber(String crdNumber) {
		this.crdNumber = crdNumber;
	}

	public String getCrdExpire() {
		return crdExpire;
	}

	public void setCrdExpire(String crdExpire) {
		this.crdExpire = crdExpire;
	}

	public String getCrdCvv() {
		return crdCvv;
	}

	public void setCrdCvv(String crdCvv) {
		this.crdCvv = crdCvv;
	}

	public String getCrdCfname() {
		return crdCfname;
	}

	public void setCrdCfname(String crdCfname) {
		this.crdCfname = crdCfname;
	}

	public String getCrdClname() {
		return crdClname;
	}

	public void setCrdClname(String crdClname) {
		this.crdClname = crdClname;
	}

	public String getCrdCphone() {
		return crdCphone;
	}

	public void setCrdCphone(String crdCphone) {
		this.crdCphone = crdCphone;
	}

	public String getCrdCemail() {
		return crdCemail;
	}

	public void setCrdCemail(String crdCemail) {
		this.crdCemail = crdCemail;
	}

	public String getCrdCcountry() {
		return crdCcountry;
	}

	public void setCrdCcountry(String crdCcountry) {
		this.crdCcountry = crdCcountry;
	}

	public String getCrdCstate() {
		return crdCstate;
	}

	public void setCrdCstate(String crdCstate) {
		this.crdCstate = crdCstate;
	}

	public String getCrdCcity() {
		return crdCcity;
	}

	public void setCrdCcity(String crdCcity) {
		this.crdCcity = crdCcity;
	}

	public String getCrdCaddr() {
		return crdCaddr;
	}

	public void setCrdCaddr(String crdCaddr) {
		this.crdCaddr = crdCaddr;
	}

	public String getCrdCzip() {
		return crdCzip;
	}

	public void setCrdCzip(String crdCzip) {
		this.crdCzip = crdCzip;
	}

	public String getCrdHfname() {
		return crdHfname;
	}

	public void setCrdHfname(String crdHfname) {
		this.crdHfname = crdHfname;
	}

	public String getCrdHlname() {
		return crdHlname;
	}

	public void setCrdHlname(String crdHlname) {
		this.crdHlname = crdHlname;
	}

	public String getCrdHphone() {
		return crdHphone;
	}

	public void setCrdHphone(String crdHphone) {
		this.crdHphone = crdHphone;
	}

	public String getCrdHemail() {
		return crdHemail;
	}

	public void setCrdHemail(String crdHemail) {
		this.crdHemail = crdHemail;
	}

	public String getCrdHcountry() {
		return crdHcountry;
	}

	public void setCrdHcountry(String crdHcountry) {
		this.crdHcountry = crdHcountry;
	}

	public String getCrdHstate() {
		return crdHstate;
	}

	public void setCrdHstate(String crdHstate) {
		this.crdHstate = crdHstate;
	}

	public String getCrdHcity() {
		return crdHcity;
	}

	public void setCrdHcity(String crdHcity) {
		this.crdHcity = crdHcity;
	}

	public String getCrdHaddr() {
		return crdHaddr;
	}

	public void setCrdHaddr(String crdHaddr) {
		this.crdHaddr = crdHaddr;
	}

	public String getCrdHzip() {
		return crdHzip;
	}

	public void setCrdHzip(String crdHzip) {
		this.crdHzip = crdHzip;
	}

	public String getCrdState() {
		return crdState;
	}

	public void setCrdState(String crdState) {
		this.crdState = crdState;
	}

	public Date getCrdDate() {
		return crdDate;
	}

	public void setCrdDate(Date crdDate) {
		this.crdDate = crdDate;
	}

	public Date getCrdDateTop() {
		return crdDateTop;
	}

	public void setCrdDateTop(Date crdDateTop) {
		this.crdDateTop = crdDateTop;
	}

	public Date getCrdDateEnd() {
		return crdDateEnd;
	}

	public void setCrdDateEnd(Date crdDateEnd) {
		this.crdDateEnd = crdDateEnd;
	}

	public String getCrdRemark() {
		return crdRemark;
	}

	public void setCrdRemark(String crdRemark) {
		this.crdRemark = crdRemark;
	}

}
